extends Node

@onready var board: Node = $"../Board" as Board
@onready var ui: CanvasLayer = $"../UI" as UI

var current_tetromino
var next_tetromino
var is_game_over = false

#方片生成器
func _ready() -> void:
	#在生成器的内部，随机获取一个当前的方块类型，使用random函数来获取即可
	current_tetromino = Shared.Tetromino.values().pick_random()
	next_tetromino= Shared.Tetromino.values().pick_random()
	
	#然后传入borad内部的生成函数，指定当前传入的类型，下一块，以及生成位置
	board.spawn_tetromino(current_tetromino,false,null)
	board.spawn_tetromino(next_tetromino,true,Vector2(100,50))
	#链接信号
	board.tetromino_locked.connect(on_tetromino_lock)
	board.game_over.connect(on_game_over)
	
	#每放置完毕一个方块，就接受一次信号，执行对应的信号函数，让棋盘生成下一块方块
func on_tetromino_lock():
	#print("已收到停滞信号")
	if is_game_over:
		return 
	current_tetromino = next_tetromino
	next_tetromino= Shared.Tetromino.values().pick_random()
	var new_tetromino = Shared.Tetromino.values().pick_random()
	board.spawn_tetromino(new_tetromino,false,null)
	board.spawn_tetromino(next_tetromino,true,Vector2(100,50))

func on_game_over() :
	print("游戏结束信号")
	is_game_over = true
	ui.show_game_over()
